package com.gxa.autoparts.admin.service.impl;

import com.gxa.autoparts.admin.mapper.OrderMapper;
import com.gxa.autoparts.admin.mapper.UserMapper;
import com.gxa.autoparts.admin.service.UserService;
import com.gxa.autoparts.core.pojo.User;
import com.gxa.autoparts.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    OrderMapper orderMapper;

    @Override
    public List<Map> selectRecharge(Integer page, Integer size) throws Exception {
        List<Map> users = userMapper.selectRecharge((page - 1) * size, size);
        if(users==null){
            throw new Exception("没有查询到数据");
        }
        return users;
    }

    @Override
    public List<Map> selectRechargeByTime(String time1, String time2, Integer page, Integer size) throws Exception {
        List<Map> users = userMapper.selectRechargeByTime(time1, time2, (page - 1) * size, size);
        if(users==null){
            throw new Exception("没有查询到数据");
        }
        return users;
    }

    @Override
    public List<Map> selectOrderAndUser(Integer page, Integer size) throws Exception {
        List<Map> users = orderMapper.selectOrderAndUser((page - 1) * size, size);
        if(users==null){
            throw new Exception("没有查询到数据");
        }
        return users;
    }

    @Override
    public List<Map> selectOrderAndUserBy(String seriaLnNumber, String time1, String time2, Integer page, Integer size) throws Exception {
        List<Map> users = orderMapper.selectOrderAndUserBy(seriaLnNumber, time1, time2, (page - 1) * size, size);
        if(users==null){
            throw new Exception("没有查询到数据");
        }
        return users;
    }

    @Override
    public List<User> selectAll(Integer page, Integer size) throws Exception {
        List<User> users = userMapper.selectAll1((page - 1) * size, size);
        if(users==null){
            throw new Exception("没有查询到数据");
        }
        return users;
    }

    @Override
    public void updateState(Long id) {
        User user = super.selectById(id);
        Boolean state = user.getState();
        if(state){
            user.setState(false);
        }
        if(!state){
            user.setState(true);
        }
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<User> selectByNameAndPhoneAndState(String name, String phone, Boolean state, Integer page, Integer size) throws Exception {
        List<User> users = userMapper.selectByNameAndPhoneAndState(name, phone, state, (page-1)*size, size);
        if(users==null){
            throw new Exception("没有查询到数据");
        }
        return users;
    }
}
